package com.example.backend.controller;

import com.example.backend.entity.UserAdress;
import com.example.backend.service.UserAdressService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;

@RestController
@CrossOrigin
public class UserAdressCotroller {

    @Autowired
    private UserAdressService userAdressService;

    @PostMapping("/user_adress")
    public int doAdd(@RequestBody UserAdress record) {
        if(record.getIs_default() == 1)
            userAdressService.updateNoDefault(record);
        return userAdressService.insert(record);
    }

    @PutMapping("/user_adress")
    public int doUpdate(@RequestBody UserAdress record) {
        if(record.getIs_default() == 1)
            userAdressService.updateNoDefault(record);
        return userAdressService.update(record);
    }

    @DeleteMapping("/user_adress/{id}")
    public int doDelete(@PathVariable("id") String id) {
        return userAdressService.deleteById(id);
    }

    @GetMapping("/user_adress/{id}")
    public UserAdress doGet(@PathVariable("id") Integer id) {
        return userAdressService.selectById(id);
    }

    @GetMapping("/user_adress/user/{user_id}")
    public ArrayList<UserAdress> doGetByUserId(@PathVariable("user_id") Integer user_id) {
        return userAdressService.selectByUserId(user_id);
    }

    @GetMapping("/user_adress")
    public ArrayList<UserAdress> doGetAll() {
        return userAdressService.selectAll();
    }

}
